package blog.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import blog.entity.Page;
import blog.service.PageService;

@Controller
@RequestMapping("/page")
public class PageController {
	@Autowired
	private PageService pageService;
	/**
	 * 去页面主界面
	 * @param map
	 * @return
	 */
	@RequestMapping("")
	public String pageList(ModelMap map) {
		List<Page> findAllList = pageService.findAllList();
		map.put("pageList", findAllList);
		return "page/page_list";
	}
	/**
	 * 去添加页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.GET)
	public String pageGoAdd(ModelMap map) {
		return "page/page_add";
	}
	/**
	 * 添加页面
	 * @param map
	 * @param page
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	public String pageAdd(ModelMap map,Page page) {
		page.setPageStatus(1);
		page.setPageCreateTime(new Date());
		pageService.addPage(page);
		return "forward:/page";
	}
	/**
	 * 去修改界面
	 * @param pageId
	 * @return
	 */
	@RequestMapping("/edit/{pageId}")
	public String pageGoEdit(ModelMap map,@PathVariable("pageId") Integer pageId) {
		Page  page =pageService.getPageById(pageId);
		map.put("page", page);
		return "page/page_edit";
	}
	/**
	 * 修改页面
	 * @param page
	 * @return
	 */
	@RequestMapping("/edit")
	public String pageEdit(Page page) {
		pageService.editPage(page);
		return "forward:/page";
	}
	@RequestMapping("/delete/{pageId}")
	public String pageDelete(@PathVariable("pageId")Integer pageId) {
		pageService.pageDelete(pageId);
		return "forward:/page";
	}
}
